package com.taldh.design.patterns.proxy.dynamic;

import java.lang.reflect.Proxy;

/**
 * @author ydh
 * @date 2020/5/14 7:21 下午
 **/

public class JdkProxyTest {

    public interface A {
        void test();
    }

    public static class B implements A {

        @Override
        public void test() {
            System.out.println("This is B");
        }
    }

    public static void main(String[] args) {
        //jdk8及之前：
        //System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        //jdk8之后：
        System.setProperty("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");

        A a = new B();
        a.test();
        MyInvocationHandler invocationHandler = new MyInvocationHandler(a);

        A instance = (A) Proxy.newProxyInstance(a.getClass().getClassLoader(),
                a.getClass().getInterfaces(), invocationHandler);

        instance.test();

    }

}
